//
//  MBGoodsTypeView.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/23.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBGoodsTypeView.h"

@interface MBGoodsTypeView ()
@property (nonatomic,weak)UIView* bottomLine;

@property (nonatomic,strong) NSMutableArray* buttonArray;

@end

@implementation MBGoodsTypeView

- (instancetype)initWithTypeArray:(NSArray*)array
{
    self = [super init];
    if (self) {
        CAGradientLayer *gradientLayer0 = [[CAGradientLayer alloc] init];
        gradientLayer0.frame = CGRectMake(0, 0, SCREEN_WIDTH, [UIView lf_sizeFromIphone6:44]);
        gradientLayer0.colors = @[
                                  (id)[UIColor colorWithRed:255.0f/255.0f green:136.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor,
                                  (id)[UIColor colorWithRed:255.0f/255.0f green:99.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor];
        gradientLayer0.locations = @[@0, @1];
        [gradientLayer0 setStartPoint:CGPointMake(0, 0)];
        [gradientLayer0 setEndPoint:CGPointMake(1, 0)];
        [self.layer addSublayer:gradientLayer0];
        
        CGFloat width = (SCREEN_WIDTH-[UIView lf_sizeFromIphone6:30])/array.count;
        
        for (int i=0; i<array.count; i++) {
            UIButton* typeButton = [[UIButton alloc]init];
            [self.buttonArray addObject:typeButton];
            [typeButton setTitle:array[i] forState:UIControlStateNormal];
            typeButton.titleLabel.font = [UIFont lf_systemFontOfSize:14];
            [typeButton setTitleColor:[UIColor colorWithR:120 G:120 B:120] forState:UIControlStateNormal];
            [typeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            [typeButton addTarget:self action:@selector(handleClickTypeButton:) forControlEvents:UIControlEventTouchUpInside];
            typeButton.tag = i;
            [self addSubview:typeButton];
            
            [typeButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self);
                make.left.equalTo(self).offset([UIView lf_sizeFromIphone6:15]+width*i);
                make.width.mas_equalTo([UIView lf_sizeFromIphone6:115]);
                make.height.mas_equalTo([UIView lf_sizeFromIphone6:44]);
            }];
            
            if (i == 0) {
                typeButton.selected = YES;
                
                UIView* bottomLine = [[UIView alloc]init];
                self.bottomLine = bottomLine;
                bottomLine.backgroundColor = [UIColor whiteColor];
                [self addSubview:bottomLine];
                
                [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.bottom.centerX.equalTo(typeButton);
                    make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:60], [UIView lf_sizeFromIphone6:2]));
                }];
            }
            
            if (i != array.count -1) {
                UIView* line = [[UIView alloc]init];
                line.backgroundColor = [UIColor whiteColor];
                [self addSubview:line];
                
                [line mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.centerY.equalTo(typeButton);
                    make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:1], [UIView lf_sizeFromIphone6:14]));
                }];
            }
        }
    }
    return self;
}

-(NSMutableArray *)buttonArray
{
    if (_buttonArray == nil) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

-(void)handleClickTypeButton:(UIButton*)button
{
    button.selected = YES;
    if (button.tag != self.type) {
        UIButton* unButton = self.buttonArray[self.type];
        unButton.selected = NO;
        
        [self.bottomLine mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.centerX.equalTo(button);
            make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:60], [UIView lf_sizeFromIphone6:2]));
        }];
        self.type = (int)button.tag;
    }
}

@end
